home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / util / netupds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-29  |  3.8 KB  |  191 lines

  1. /* @(#) $Header: netupds.c,v 1.6 91/10/25 14:21:27 deyke Exp $ */
  2.  
  3. /* Net Update Server */
  4.  
  5. #define _HPUX_SOURCE    1
  6.  
  7. #include <sys/types.h>
  8.  
  9. #include <ctype.h>
  10. #include <fcntl.h>
  11. #include <netinet/in.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16.  
  17. #if defined(__TURBOC__) || defined(__STDC__)
  18. #define __ARGS(x)       x
  19. #else
  20. #define __ARGS(x)       ()
  21. #define const
  22. #endif
  23.  
  24. static void pexit __ARGS((const char *s));
  25. static void doread __ARGS((int fd, char *buf, size_t cnt));
  26. static void dowrite __ARGS((int fd, const char *buf, size_t cnt));
  27. int main __ARGS((void));
  28.  
  29. /*---------------------------------------------------------------------------*/
  30.  
  31. static void pexit(s)
  32. const char *s;
  33. {
  34.   perror(s);
  35.   exit(1);
  36. }
  37.  
  38. /*---------------------------------------------------------------------------*/
  39.  
  40. static void doread(fd, buf, cnt)
  41. int  fd;
  42. char  *buf;
  43. size_t cnt;
  44. {
  45.  
  46.   char  *p = buf;
  47.   int  n;
  48.  
  49.   while (cnt) {
  50.     n = read(fd, p, cnt);
  51.     if (n < 0) pexit("read");
  52.     if (!n) {
  53.       printf("read(): End of file\n");
  54.       exit(1);
  55.     }
  56.     p += n;
  57.     cnt -= n;
  58.   }
  59. }
  60.  
  61. /*---------------------------------------------------------------------------*/
  62.  
  63. static void dowrite(fd, buf, cnt)
  64. int  fd;
  65. const char *buf;
  66. size_t cnt;
  67. {
  68.  
  69.   const char * p = buf;
  70.   int  n;
  71.  
  72.   while (cnt) {
  73.     n = write(fd, p, cnt);
  74.     if (n <= 0) pexit("write");
  75.     p += n;
  76.     cnt -= n;
  77.   }
  78. }
  79.  
  80. /*---------------------------------------------------------------------------*/
  81.  
  82. int  main()
  83. {
  84.  
  85.   char  buf[1024];
  86.   char  client[1024];
  87.   char  filename[1024];
  88.   int  fdfile;
  89.   int  fdpipe[2];
  90.   int  fdsocket;
  91.   int  filesize;
  92.   int  i;
  93.   int  net_filesize;
  94.   int  net_i;
  95.   struct stat statbuf;
  96.  
  97.   alarm(6 * 3600);
  98.  
  99.   umask(022);
  100.   putenv("HOME=/users/root");
  101.   putenv("LOGNAME=root");
  102.   putenv("PATH=/bin:/usr/bin:/usr/local/bin:/usr/contrib/bin");
  103.   putenv("SHELL=/bin/sh");
  104.   putenv("TZ=MEZ-1MESZ");
  105.  
  106.   if ((fdsocket = dup(0)) < 3) exit(1);
  107.   if (pipe(fdpipe)) exit(1);
  108.   switch (fork()) {
  109.   case -1:
  110.     exit(1);
  111.   case 0:
  112.     for (i = 0; i < _NFILE; i++)
  113.       if (i != fdpipe[0]) close(i);
  114.     dup(fdpipe[0]);
  115.     open("/dev/null", O_RDWR, 0666);
  116.     open("/dev/null", O_RDWR, 0666);
  117.     close(fdpipe[0]);
  118.     execl("/usr/bin/mailx", "mailx", "-s", "netupds log", "root", (char *) 0);
  119.     exit(1);
  120.   default:
  121.     for (i = 0; i < _NFILE; i++)
  122.       if (i != fdpipe[1] && i != fdsocket) close(i);
  123.     open("/dev/null", O_RDWR, 0666);
  124.     dup(fdpipe[1]);
  125.     dup(fdpipe[1]);
  126.     close(fdpipe[1]);
  127.   }
  128.  
  129.   for (i = 0; ; i++) {
  130.     if (i >= sizeof(client)) {
  131.       printf("Client name too long\n");
  132.       exit(1);
  133.     }
  134.     doread(fdsocket, client + i, 1);
  135.     if (!client[i]) break;
  136.     if (!isalnum(client[i] & 0xff) && client[i] != '-') {
  137.       printf("Bad char in client name\n");
  138.       exit(1);
  139.     }
  140.   }
  141.   if (!*client) {
  142.     printf("Null client name\n");
  143.     exit(1);
  144.   }
  145.  
  146.   printf("Client = %s\n", client);
  147.   fflush(stdout);
  148.  
  149.   sprintf(buf, "/users/funk/dk5sg/tcp.%s", client);
  150.   if (chdir(buf)) pexit(buf);
  151.  
  152.   tmpnam(filename);
  153.   sprintf(buf, "/users/funk/dk5sg/tcp/util/genupd %s | compress > %s", client, filename);
  154.   system(buf);
  155.  
  156.   if (stat(filename, &statbuf)) pexit(filename);
  157.   filesize = statbuf.st_size;
  158.  
  159.   printf("File size = %i\n", filesize);
  160.   fflush(stdout);
  161.  
  162.   net_filesize = htonl(filesize);
  163.   dowrite(fdsocket, (char *) &net_filesize, 4);
  164.  
  165.   fdfile = open(filename, O_RDONLY, 0600);
  166.   if (fdfile < 0) pexit(filename);
  167.   while (filesize > 0) {
  168.     i = filesize < sizeof(buf) ? filesize : sizeof(buf);
  169.     doread(fdfile, buf, (unsigned) i);
  170.     dowrite(fdsocket, buf, (unsigned) i);
  171.     filesize -= i;
  172.   }
  173.   if (close(fdfile)) pexit("close");
  174.  
  175.   doread(fdsocket, (char *) &net_i, 4);
  176.   i = ntohl(net_i);
  177.  
  178.   printf("Response = %i\n", i);
  179.   fflush(stdout);
  180.  
  181.   if (!i) {
  182.     sprintf(buf, "uncompress < %s | sh", filename);
  183.     system(buf);
  184.   }
  185.  
  186.   if (unlink(filename)) pexit(filename);
  187.  
  188.   return 0;
  189. }
  190.  
  191.